<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Global Mall</title>
  <link rel="icon" href="assets/image/logo/favicon.png" type="image/png" sizes="32x32">
  <link href="assets/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="assets/css/owl.carousel.min.css">
  <link rel="stylesheet" type="text/css" href="assets/css/owl.theme.default.min.css">
  <link rel="stylesheet" type="text/css" href="assets/fonts/css2.css">
  <link rel="stylesheet" type="text/css" href="assets/css/jquery.fancybox.min.css">
  <link rel="stylesheet" type="text/css" href="assets/css/all.min.css">
  <link rel="stylesheet" type="text/css" href="assets/css/style.css">
  <link rel="stylesheet" type="text/css" href="assets/css/responsive.css">
</head>

<body>
  <div class="preloader"></div>
  <?php
  include 'header.php';
  ?>
  <!-- checkout -->
  <div class="mb-3 mt-3">
    <div class="container">
      <div class="row">
        <div class="col-12">
          <ul class="breadcums">
            <li class="d-inline-block font-weight-bolder button-group2">
              <a href="index.php" class="login_txt symbol2 pro_home"></a>
            </li>
            <li class="d-inline-block hr_ font-weight-bolder"><a href="#" class="login_txt">订 单 信 息</a></li>
          </ul>
        </div>
      </div>
    </div>
  </div>
  <!-- checkout form -->
  <?php
  if (!isset($_SESSION['user_id'])) {
    echo "<script>alert('请先登录！');window.location.href='login.php';</script>";
    exit;
  }
  $user_id = $_SESSION['user_id']; // 从session获取用户ID

  // 获取用户的收货地址
  $sql_address = "SELECT name, phone, address FROM users WHERE id = ?";
  $stmt = $conn->prepare($sql_address);
  $stmt->bind_param("i", $user_id);
  $stmt->execute();
  $result_address = $stmt->get_result();
  $user_address = $result_address->fetch_assoc();

  // 获取购物车商品信息
  $sql_cart = "SELECT p.name, p.price, c.quantity, c.total FROM cart c
             JOIN products p ON c.product_id = p.id
             WHERE c.user_id = ?";
  $stmt = $conn->prepare($sql_cart);
  $stmt->bind_param("i", $user_id);
  $stmt->execute();
  $result_cart = $stmt->get_result();

  // 计算总金额和运费
  $subtotal = 0;
  $shipping = 9.00; // 假设运费为固定值 $9.00
  while ($row = $result_cart->fetch_assoc()) {
    $subtotal += $row['total'];
  }
  $taxes = $subtotal * 0.1; // 假设税费为总金额的10%
  $total = $subtotal + $shipping + $taxes;

  $stmt->close();
  ?>

  <!-- HTML 页面 -->
  <div id="checkout_page" class="checkout-page animate__animated animate__fadeInUp">
    <div class="container custom_container">
      <div class="row">
        <div class="col-lg-8 col-12">
          <div class="accordion" id="check_out_toggle">
            <!-- Address -->
            <div class="card mb-2">
              <div class="card-header bg-white" id="chechout_add">
                <h2 class="mb-0">
                  <button class="btn btn-link btn-block text-left collapsed text-body p-0 font-weight-bold" type="button" data-toggle="collapse" data-target="#check_add" aria-expanded="true" aria-controls="check_add">
                    收 货 信 息<span class="float-right"><i class="fas fa-angle-down"></i></span>
                  </button>
                </h2>
              </div>
              <div id="check_add" class="collapse show" aria-labelledby="chechout_add" data-parent="#check_out_toggle">
                <div class="card-body">
                  <div id="c_address" class="page-content">
                    <form>
                      <div class="form-group">
                        <label for="f_name" class="font-weight-bolder nowant">收件人姓名</label>
                        <input type="text" class="form-control emaill" value="<?= $user_address['name'] ?>" disabled>
                        <div class="invalid-feedback">收件人姓名.</div>
                      </div>
                      <div class="form-group">
                        <label for="l_name" class="font-weight-bolder nowant">联系方式</label>
                        <input type="text" class="form-control emaill" value="<?= $user_address['phone'] ?>" disabled>
                        <div class="invalid-feedback">联系方式.</div>
                      </div>
                      <div class="form-group">
                        <label for="r_email" class="font-weight-bolder nowant">地址</label>
                        <input type="text" class="form-control emaill" value="<?= $user_address['address'] ?>" disabled>
                        <div class="invalid-feedback">地址信息.</div>
                      </div>
                    </form>
                  </div>
                </div>
              </div>
            </div>

            <!-- Payment Card -->
            <div class="card mb-2">
              <div class="card-header bg-white" id="checkout_payment">
                <h2 class="mb-0">
                  <button class="btn btn-link btn-block text-left collapsed text-body p-0 font-weight-bold" type="button" data-toggle="collapse" data-target="#check_payment" aria-expanded="true" aria-controls="check_payment">
                    支 付 方 式<span class="float-right"><i class="fas fa-angle-down"></i></span>
                  </button>
                </h2>
              </div>
              <div id="check_payment" class="collapse show" aria-labelledby="checkout_payment" data-parent="#check_out_toggle">
                <div class="card-body">
                  <div id="pay_check" class="page-content">
                    <form action="checkout_process.php" method="post">
                      <div class="form-group text-left">
                        <label class="font-weight-bolder nowant">支付方式</label><br>
                        <span class="form-check d-inline-block">
                          <input class="form-check-input" type="radio" name="payment_method" id="pay_by_check" value="支付宝" checked>
                          <label class="form-check-label nowant" for="pay_by_check">
                            支付宝
                          </label>
                        </span>
                        <span class="form-check d-inline-block ml-2">
                          <input class="form-check-input" type="radio" name="payment_method" id="pay_by_bank" value="微信支付">
                          <label class="form-check-label nowant" for="pay_by_bank">
                            微信支付
                          </label>
                        </span>
                      </div>

                      <div class="text-center">
                        <button type="submit" class="btn checkout_btn">
                          确认付款
                        </button>
                      </div>
                    </form>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- Order Summary -->
        <div class="col-lg-4 col-12">
          <div class="border bg-white bill_pay">
            <div class="card-body border-bottom">
              <p class="text-muted">共 <?= mysqli_num_rows($result_cart) ?> 件商品</p>
              <p class="font-weight-bolder">订单详情</p>
              <?php
              mysqli_data_seek($result_cart, 0);
              ?>
              <div>
                <span class="font-weight-bold">商品金额</span>
                <span class="float-right font-weight-bold">￥<?= number_format($subtotal, 2) ?></span>
              </div>
              <div>
                <span class="font-weight-bold">运费</span>
                <span class="float-right font-weight-bold">￥0.00</span>
              </div>
            </div>
            <div class="card-body">
              <div>
                <span class="font-weight-bold">总金额</span>
                <span class="float-right font-weight-bold">￥<?= number_format($subtotal, 2) ?></span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- Footer Section Start -->
  <?php
  include 'footer.php';
  ?>
  </script>
  <!-- jQuery v3.5.1 -->
  <script src="assets/js/jquery-3.4.1.min.js"></script>
  <!-- jQuery OwlCarousel v1.3.3 -->
  <script src="assets/js/owl.carousel.js"></script>
  <!-- popper v5.0 -->
  <script src="assets/js/popper.min.js"></script>
  <!-- Bootstrap v5.0 -->
  <script src="assets/js/bootstrap.min.js"></script>
  <!-- jQuery fancybox js -->
  <script src="assets/js/jquery.fancybox.min.js"></script>
  <!-- custom file js -->
  <script src="assets/js/custom.js"></script>
  <!-- aos js -->
  <script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
</body>

</html>